/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.yuntongxun.ecdemo.ui.group;


import android.text.TextUtils;

import com.yuntongxun.ecdemo.common.CCPAppManager;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.storage.GroupMemberSqlManager;
import com.yuntongxun.ecdemo.storage.GroupNoticeSqlManager;
import com.yuntongxun.ecdemo.storage.GroupSqlManager;
import com.yuntongxun.ecdemo.storage.IMessageSqlManager;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecsdk.im.ECGroup;
import com.yuntongxun.ecsdk.im.group.ECChangeAdminMsg;
import com.yuntongxun.ecsdk.im.group.ECChangeMemberRoleMsg;
import com.yuntongxun.ecsdk.im.group.ECDismissGroupMsg;
import com.yuntongxun.ecsdk.im.group.ECGroupNoticeMessage;
import com.yuntongxun.ecsdk.im.group.ECInviterMsg;
import com.yuntongxun.ecsdk.im.group.ECJoinGroupMsg;
import com.yuntongxun.ecsdk.im.group.ECModifyGroupMsg;
import com.yuntongxun.ecsdk.im.group.ECProposerMsg;
import com.yuntongxun.ecsdk.im.group.ECQuitGroupMsg;
import com.yuntongxun.ecsdk.im.group.ECRemoveMemberMsg;
import com.yuntongxun.ecsdk.im.group.ECReplyInviteGroupMsg;
import com.yuntongxun.ecsdk.im.group.ECReplyJoinGroupMsg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author Jorstin Chan@容联•云通讯
 * @date 2014-12-31
 * @version 4.0
 */
public class GroupNoticeHelper {

	/**群组通知接口*/
	private OnPushGroupNoticeMessageListener mListener;
	
	private static GroupNoticeHelper mHelper;
	private static GroupNoticeHelper getHelper() {
		if(mHelper == null) {
			mHelper = new GroupNoticeHelper();
		}
		return mHelper;
	} 
	


	/**
	 * 用户申请加入群组 - PUSH到群组管理员
	 * @param notice
	 */
	private NoticeSystemMessage onIMProposerMsg(ECProposerMsg notice) {
		NoticeSystemMessage systemMessage = createNoticeSystemMessage(notice);
		systemMessage.setMember(notice.getProposer());
		if(notice.isDiscuss()){
			
			systemMessage.setContent("<member>"+notice.getProposer()+ "</member> 加入了讨论组");
		}else {
			
			systemMessage.setContent("<member>"+notice.getProposer()+ "</member> 加入了群组");
		}
		systemMessage.setDateCreated(notice.getDateCreated());
		
		GroupService.syncGroupInfo(notice.getGroupId());
		return systemMessage;
		
	}


	
	/**
	 * 生成群组通知消息
	 * @return
	 */
	private NoticeSystemMessage createNoticeSystemMessage(ECGroupNoticeMessage notice) {
		NoticeSystemMessage message = new NoticeSystemMessage(notice.getType());
		message.setGroupId(notice.getGroupId());
		message.setIsRead(IMessageSqlManager.IMESSENGER_TYPE_UNREAD);
		return message;
	}
	
	public static void addListener(OnPushGroupNoticeMessageListener listener) {
		getHelper().mListener = listener;
	}
	
	/**
	 * @param content
	 * @return
	 */
	public static CharSequence getNoticeContent(String content) {
		if(content == null) {
			return content;
		}
		if(content.indexOf("<admin>") != -1 && content.indexOf("</admin>") != -1) {
			int start = content.indexOf("<admin>");
			int end = content.indexOf("</admin>");
			String contactId = content.substring(start + "<admin>".length(), end);
			ECContacts contact = ContactSqlManager.getContact(contactId);
			String target = content.substring(start, end + "</admin>".length());
			content = content.replace(target, contact.getNickname());
		} 
		if(content.indexOf("<member>") != -1 && content.indexOf("</member>") != -1) {
			int start = content.indexOf("<member>");
			int end = content.indexOf("</member>");
			String member = content.substring(start + "<member>".length(), end);
			ECContacts contact = ContactSqlManager.getContact(member);
			String target = content.substring(start, end + "</member>".length());
			content = content.replace(target, contact.getNickname());
		}
		if(content.indexOf("<groupId>") != -1 && content.indexOf("</groupId>") != -1) {
			int start = content.indexOf("<groupId>");
			int end = content.indexOf("</groupId>");
			String groupId = content.substring(start + "<groupId>".length(), end);
			ECGroup ecGroup = GroupSqlManager.getECGroup(groupId);
			String target = content.substring(start, end + "</groupId>".length());
			if(ecGroup == null) {
				GroupService.syncGroupInfo(groupId);
			}
			content = content.replace(target, ecGroup!= null ? ecGroup.getName() : "");
		}
		return content;
	}



    /***********************************************************************************************/

    public static final int SYSTEM_MESSAGE_NEED_REPLAY = 1;
    public static final int SYSTEM_MESSAGE_NONEED_REPLAY = 2;
    public static final int SYSTEM_MESSAGE_THROUGH = 3;
    public static final int SYSTEM_MESSAGE_REFUSE = 4;


	
	/**
	 * 通知
	 * @param system
	 */
	private void notify(DemoGroupNotice system) {
		if(getHelper().mListener != null) {
			getHelper().mListener.onPushGroupNoticeMessage(system);
		}
	}
	
	/**
	 * 群组通知
	 */
	public interface OnPushGroupNoticeMessageListener {
		void onPushGroupNoticeMessage(DemoGroupNotice system);
	}
}
